<?php
require_once (APPLICATION_DIR. "/library/external/XOAD/xoad.php");
class AlumniController extends DRX_Controller 
{		
	function xoadGetMeta()
	{
		XOAD_Client::mapMethods($this,array("login", "changepersonal", "changecontact", "recommend", "receivenewsletter"));
	}
	
	function unsubcribeAction()
	{
		$objCront = new Cront();
		$strGet = isset($_GET["ami"]) ? $_GET["ami"] : "";
		if ( $strGet == "" )
			redirect(APPLICATION_URL . "alumni");
		$arrGet = explode("-", $strGet);
		if ( count($arrGet) != 3)
			redirect(APPLICATION_URL . "alumni");
		
		///check thong tin dua tren link trong email
		if ( $arrInfoCheck = $objCront -> getInfoCheckUnsucribe($arrGet[0], $arrGet[1], $arrGet[2]) )
		{
			$objAlumni = new Alumni();
			$objAlumni -> setReceiveNewsletter($arrGet[1], 0);
		}
		
		redirect(APPLICATION_URL . "alumni");
	}


/*====================================================================================================
==============================          CLASS MATE                 =========================================*/
	function receivenewsletter($p_arrArgs)
	{
		$objAlumni = new Alumni();
		if ( !isset($_SESSION["MEMBER_LOGIN"]) || $_SESSION["MEMBER_LOGIN"]["id"] < 0 )
		{
			return 0;
		}
		if ( $objAlumni -> setReceiveNewsletter($_SESSION["MEMBER_LOGIN"]["id"], $p_arrArgs['value']) )
		{
			$arrInfoMember = $objAlumni -> getInfoMemberId($_SESSION["MEMBER_LOGIN"]["id"]);
			$_SESSION["MEMBER_LOGIN"] = $arrInfoMember;
			return 1;
		}
		else
			return 2;
	}
	
	
	function recommend($p_arrArgs)
	{
		$objAlumni = new Alumni();
		if ( !isset($_SESSION["MEMBER_LOGIN"]) || $_SESSION["MEMBER_LOGIN"]["id"] < 0 )
		{
			return 0;
		}
		else if ( $_SESSION["MEMBER_LOGIN"]["id"] == $p_arrArgs['mb_id'] )
			return 0;
			
		//check xem da recomment cho profile nay chua
		if ( $objAlumni -> checkMemberRecommend($p_arrArgs['mb_id'], $_SESSION["MEMBER_LOGIN"]["id"]) )
			return 0;
		
		if ( $objAlumni -> recommendProfile($p_arrArgs['mb_id'], $_SESSION["MEMBER_LOGIN"]["id"]) )
		{
			$objAlumni -> incrementRecommendProfile($p_arrArgs['mb_id']);
			return 1;
		}
		else
			return 2;
	}
	
	function detailProfile()
	{
		global $arrArgs;
		$smarty 	= DRX::load("smarty");
		$objAlumni = new Alumni();
		$intId = isset($arrArgs[1]) && is_numeric($arrArgs[1]) ? $arrArgs[1] : 0;
		
		if ( !$intId )
		{
			if ( isset($_SESSION["BACK_URL"]) )
				redirect($_SESSION["BACK_URL"]);
			else
				redirect(APPLICATION_URL . "alumni/classmate");
		}
		
		if ( isset($_SESSION["MEMBER_LOGIN"]) && $_SESSION["MEMBER_LOGIN"]["id"] > 0 && $_SESSION["MEMBER_LOGIN"]["id"] == $intId)
		{
			redirect(APPLICATION_URL . "alumni/profile/completion");
		}
		
		$arrMemberPersonal = $objAlumni -> getInfoMemberId($intId);
		$arrMemberContact = $objAlumni -> getInfoMemberContact($intId);
		$arrMemberCurrentPosition = $objAlumni -> getListCurrentPosition($intId);
		$arrMemberPastPosition = $objAlumni -> getListPastPosition($intId);
		//echo '<pre>';var_dump($arrMemberPersonal);
		$smarty->assign("arrMemberPersonal",$arrMemberPersonal);
		$smarty->assign("arrMemberContact",$arrMemberContact);
		$smarty->assign("arrMemberCurrentPosition",$arrMemberCurrentPosition);
		$smarty->assign("arrMemberPastPosition",$arrMemberPastPosition);		
		
		$content = $smarty->fetch("alumni/classmate/view_profile.tpl");
		$smarty->assign("content",$content);
		$smarty->display("solvay.tpl");
	}
		
	
	function searchClassMate()
	{
		$objSmarty 	= DRX::load("smarty");
		$objClassMate = new Alumni();
		$strQuery = $_SERVER['QUERY_STRING'];
		$arrForm = $_GET;
		
		define("RESULT_PER_PAGE_CLASSMATE", 20);
		$arrForm['page'] = isset($_GET['page']) ? $_GET['page'] : 1;
		$strQuery = str_replace("&page=".$arrForm['page'], "", $strQuery);
		
		$arrForm['start'] = ($arrForm['page']-1)*RESULT_PER_PAGE_CLASSMATE;
		$arrForm['limit'] = RESULT_PER_PAGE_CLASSMATE;
		
		$flgLogin = 0;
		if ( isset($_SESSION["MEMBER_LOGIN"]) && $_SESSION["MEMBER_LOGIN"]["id"] > 0 )
		{
			$flgLogin = $_SESSION["MEMBER_LOGIN"]["id"];
		}
		
		
		$objResultsClassMate = $objClassMate -> searchClassMate($arrForm, $flgLogin);
		//echo '<pre>';var_dump($objResultsClassMate -> fetchList());
		//phan trang
		include "IO_Page.php";
		$intTotal= $objResultsClassMate -> count('distinct');
		$objNaviPage = new IO_Page(5, RESULT_PER_PAGE_CLASSMATE, 'onPage');
		$strNaviPage = $objNaviPage -> createPageNavigatorFront($arrForm['page'], $intTotal, "");		
		$objSmarty -> assign("NAVIGATION_PAGE", $strNaviPage);
		$objSmarty -> assign("TOTAL", $intTotal);
		$objSmarty -> assign("arrResultsClassMate", $objResultsClassMate->fetchList());
		$objSmarty -> assign("intStart", $intStart);
		$objSmarty -> assign("arrForm", $arrForm);
		
		$strUrl = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
		if ( isset($_SESSION["BACK_URL"]) )
		{
			$_SESSION["BACK_URL"]	= $strUrl;
		}
		else
		{
			session_register("BACK_URL");
			$_SESSION["BACK_URL"]	= $strUrl;
		}
		
		$objSmarty -> assign("SEARCH_RESULTS", $objSmarty->fetch("alumni/classmate/search_results.tpl"));
	}

	function searchClassMateAction()
	{
		$objSmarty 	= DRX::load("smarty");
		$objGeneral = new AlumniGeneral();
		$objGeneral = new AlumniGeneral();
		
		if ( isset($_GET['full_name']) && isset($_GET['intake_id']) )
			$this -> searchClassMate();
		
		$objSmarty->assign("array_industry",$objGeneral -> getListIndustry());
		$objSmarty->assign("arrIntake",$objGeneral->getListIntake());	
		$content = $objSmarty->fetch("alumni/classmate/classmate_home_search.tpl");
		$objSmarty->assign("content",$content);
		$objSmarty->display("solvay.tpl");
	}
	
	
	function classmateAction()
	{
		global $arrArgs;
		$objSmarty 	= DRX::load("smarty");
		$layout     = new Layout();	
		$layout->prepareLayout($args);
		$this -> prepareLeft();
		
		$flgAction = isset($arrArgs[0]) ? $arrArgs[0] : 'search';
		if ( $flgAction == "search" )
		{
			$this -> searchClassMateAction();
		}
		else if ( $flgAction == "view" )
		{
			$this -> detailProfile();
		}
	}

/*====================================================================================================
==============================          CAREER                 =========================================*/
	function detailCareer()
	{
		global $arrArgs;
		$objSmarty 	= DRX::load("smarty");
		$intCareerId = isset($arrArgs[1]) ? $arrArgs[1] : '';
		$objCareer = new AlumniCareer();
		
		$arrInfoCareer = $objCareer -> getInfoCareer($intCareerId);
		
		$objSmarty -> assign("arrInfoCareer", $arrInfoCareer);
		$objSmarty->display("alumni/career/view_detail.tpl");
		
	}
	function searchCareerAction()
	{
		$objSmarty 	= DRX::load("smarty");
		$objCareer = new Alumni();
		$objAlumniCareer = new AlumniCareer();
		$objGeneral = new AlumniGeneral();
		
		$arrIndustry = $objGeneral -> getListIndustry();
		$arrLocation = $objGeneral -> getListLocation();
		$objSmarty->assign("arrIndustry",$arrIndustry);
		$objSmarty->assign("arrLocation",$arrLocation);
		
		if ( isset($_GET['industry_id']) && isset($_GET['location_id']) )
			$this -> searchCareer();
			
			
		$content = $objSmarty->fetch("alumni/career/career_home_search.tpl");
		$objSmarty->assign("content",$content);
		$objSmarty->display("solvay.tpl");
	}
	
	function searchCareer()
	{
		$objSmarty 	= DRX::load("smarty");
		$objCareer = new Alumni();
		$strQuery = $_SERVER['QUERY_STRING'];
		$arrForm = $_GET;
		
		define("RESULT_PER_PAGE_CAREER", 20);
		$arrForm['page'] = isset($_GET['page']) ? $_GET['page'] : 1;
		$strQuery = str_replace("&page=".$arrForm['page'], "", $strQuery);
		$arrForm['start'] = ($arrForm['page']-1)*RESULT_PER_PAGE_CAREER;
		$arrForm['limit'] = RESULT_PER_PAGE_CAREER;
		$objResultsCareer = $objCareer -> searchCareer($arrForm);
		
		//phan trang
		include "IO_Page.php";
		$intTotal= $objResultsCareer -> count('distinct');
		$objNaviPage = new IO_Page(5, RESULT_PER_PAGE_CAREER, 'onPage');
		$strNaviPage = $objNaviPage -> createPageNavigatorFront($arrForm['page'], $intTotal, "");		
		$objSmarty -> assign("NAVIGATION_PAGE", $strNaviPage);
		$objSmarty -> assign("TOTAL", $intTotal);
		$objSmarty -> assign("arrResultsCareer", $objResultsCareer->fetchList());
		$objSmarty -> assign("intStart", $intStart);
		$objSmarty -> assign("arrForm", $arrForm);
		
		$objSmarty -> assign("SEARCH_RESULTS", $objSmarty->fetch("alumni/career/search_results.tpl"));
	}


	function careerAction()
	{
		global $arrArgs;
		$objSmarty 	= DRX::load("smarty");
		$layout     = new Layout();	
		$layout->prepareLayout($args);
		$this -> prepareLeft();
		
		$flgAction = isset($arrArgs[0]) ? $arrArgs[0] : 'search';
		if ( $flgAction == "search" )
		{
			$this -> searchCareerAction();
		}
		else if ( $flgAction == "view" )
		{
			$this -> detailCareer();
		}
	}

/*====================================================================================================
==============================          NEWS                 =========================================*/
	function detailNews()
	{
		global $arrArgs;
		$objSmarty 	= DRX::load("smarty");
		$objNews = new Alumni();
		$intNewsId = isset($arrArgs[1]) ? $arrArgs[1] : '';
		
		//get news information
		if ( !$arrInfoNews = $objNews -> getInfoNews($intNewsId) )
		{
			redirect(APPLICATION_URL . 'alumni/news');
		}
		
		if ( isset($_POST['content']) )
		{
			$arrForm = $_POST;
			$arrForm['mb_id'] = $_SESSION["MEMBER_LOGIN"]["id"];
			$objNews -> insertNewsComment($arrForm);
			redirect(APPLICATION_URL . 'alumni/news/view/' . $intNewsId);
		}
		
		
		$objSmarty->assign("arrInfoNews",$arrInfoNews);
		///get others news
		$arrListNewsOthers = $objNews -> getListNewsOthers($intNewsId, 5);
		$objSmarty->assign("arrListNewsOthers",$arrListNewsOthers);
		
		$objSmarty->assign("list_comment",$this -> getListFrmNewsComment($intNewsId));
		$content = $objSmarty->fetch("alumni/news/view_news.tpl");
		$objSmarty->assign("content",$content);
		$objSmarty->display("solvay.tpl");
	}
	
	function getListFrmNewsComment($p_intId)
	{
		$objSmarty 	= DRX::load("smarty");
		$objNews = new Alumni();
		
		//get list news comment
		$arrListNewsComment = $objNews -> getListNewsComment($p_intId);
		$objSmarty->assign("arrListNewsComment",$arrListNewsComment);
		
		$objSmarty->assign("NEWS_ID",$p_intId);
		return $objSmarty->fetch("alumni/news/list_news_comment.tpl");
	}
	
	function listNews()
	{
		global $arrArgs;
		$objSmarty 	= DRX::load("smarty");
		$objNews = new Alumni();
		
		define("RESULT_PER_PAGE_NEWS", 21);
		
		$intPage = isset($arrArgs[1]) ? $arrArgs[1] : 1;
		$intStart = ($intPage-1)*RESULT_PER_PAGE_NEWS;
		$objListNews = $objNews -> getListNews($intStart, RESULT_PER_PAGE_NEWS);
		
		//phan trang
		include "IO_Page.php";
		$intTotal= $objListNews -> count('distinct');
		$objNaviPage = new IO_Page(5, RESULT_PER_PAGE_NEWS, 'onPage');
		$strNaviPage = $objNaviPage -> createPageNavigatorFront($intPage, $intTotal, "");		
		$objSmarty -> assign("NAVIGATION_PAGE", $strNaviPage);
		$objSmarty -> assign("TOTAL", $intTotal);
		//echo $strNaviPage;
		$objSmarty -> assign("arrListNews", $objListNews->fetchList());
		//echo '<pre>';var_dump($objListNews->fetchList());
		$objSmarty -> assign("intStart", $intStart);
		
		
		$content = $objSmarty->fetch("alumni/news/list_news.tpl");
		$objSmarty->assign("content",$content);
		$objSmarty->display("solvay.tpl");
	}
	
	function newsAction()
	{
		global $arrArgs;
		$objSmarty 	= DRX::load("smarty");
		$layout     = new Layout();	
		$layout->prepareLayout($args);
		$this -> prepareLeft();
		
		$flgAction = isset($arrArgs[0]) ? $arrArgs[0] : 'list';
		if ( $flgAction == "list" )
		{
			$this -> listNews();
		}
		else if ( $flgAction == "view" )
		{
			$this -> detailNews();
		}
	}
	
/*=========================================================================================	
===============================        PROFILE       ===========================================*/
	
	function doChangePassword()
	{
		$objSmarty 	= DRX::load("smarty");
		$objAlumni = new Alumni();
		$arrMember = $_SESSION["MEMBER_LOGIN"];
		$arrForm = $_POST;
		
		if ( trim(md5($arrForm['old_password'])) != trim($arrMember['password']) )
		{
			$objSmarty->assign("STR_MSG","Old password not match");	
			return;
		}
		if ( trim($arrForm['new_password']) != trim($arrForm['confirm_new_password']) )
		{
			$objSmarty->assign("STR_MSG","New password not match");	
			return;
		}
		
		if ( $objAlumni -> changePassword($arrMember['id'], $arrForm['new_password']) )
		{
			//change forum's password
			$this -> changePasswordOfForum($arrMember['username'], $arrForm['new_password']);
			$objSmarty->assign("STR_MSG","Change password successfully!");	
			return;
		}
		else
		{
			$objSmarty->assign("STR_MSG","Action unsuccessfully!");	
			return;
		}	
		
	}

	function changePasswordAction()
	{
		$smarty 	= DRX::load("smarty");
		$objAlumni = new Alumni();
		/*$arrMember = $_SESSION["MEMBER_LOGIN"];
		echo '<pre>'; var_dump($arrMember);*/
		
		if ( isset($_POST['old_password']) && trim($_POST['old_password']) != '' )
		{
			$this -> doChangePassword();
		}
		
		$content = $smarty->fetch("alumni/profile/frm_change_password.tpl");
		$smarty->assign("content",$content);
		$smarty->display("solvay.tpl");
	}

	
	function viewProfileCompletion()
	{
		$smarty 	= DRX::load("smarty");
		$objAlumni = new Alumni();
		
		$arrMemberContact = $objAlumni -> getInfoMemberContact($_SESSION["MEMBER_LOGIN"]["id"]);
		$arrMemberCurrentPosition = $objAlumni -> getListCurrentPosition($_SESSION["MEMBER_LOGIN"]["id"]);
		$arrMemberPastPosition = $objAlumni -> getListPastPosition($_SESSION["MEMBER_LOGIN"]["id"]);
		
		$smarty->assign("arrMemberContact",$arrMemberContact);
		$smarty->assign("arrMemberCurrentPosition",$arrMemberCurrentPosition);
		$smarty->assign("arrMemberPastPosition",$arrMemberPastPosition);		
		
		$content = $smarty->fetch("alumni/profile/view_profile_completion.tpl");
		$smarty->assign("content",$content);
		$smarty->display("solvay.tpl");
	}
	
	function editPcurrent()
	{
		$objSmarty 	= DRX::load("smarty");
		$objAlumni = new Alumni();
		$objGeneral = new AlumniGeneral();
		
		if ( isset($_POST["title"][0]) )
		{
			//truoc tien delete het nhung current position truoc do
			$objAlumni -> deleteCurrentPosition($_SESSION["MEMBER_LOGIN"]["id"]);
			$flgAction = false;
			$numberJobAdd = count($_POST["title"]);
			for( $i=0; $i<$numberJobAdd; $i++ )
			{
				$arrInfoJob = array();
				$arrInfoJob["mb_id"] = $_SESSION["MEMBER_LOGIN"]["id"];
				$arrInfoJob["title"] = $_POST["title"][$i];
				$arrInfoJob["company"] = $_POST["company"][$i];
				$arrInfoJob["website"] = $_POST["website"][$i];
				$arrInfoJob["industry_id"] = $_POST["industry_id"][$i];
				if ( trim($arrInfoJob["title"]) != '' )
				{
					$flgAction = true;
					$objAlumni -> addCurrentPosition($arrInfoJob);
				}
			}
			if ( $flgAction )
				$objAlumni -> completionProfile('complete_current_position', 10, $_SESSION["MEMBER_LOGIN"]["id"]);
			$objSmarty->assign("STR_MSG","Action successfully!");	
		}
		
		$arrListCurrentPosition = $objAlumni -> getListCurrentPosition($_SESSION["MEMBER_LOGIN"]["id"]);
		if ( count($arrListCurrentPosition) )
			$objSmarty->assign("arrListCurrentPosition",$arrListCurrentPosition);
		
		$objSmarty->assign("array_industry",$objGeneral -> getListIndustry());
		$content = $objSmarty->fetch("alumni/profile/edit_position_current.tpl");
		$objSmarty->assign("content",$content);
		$objSmarty->display("solvay.tpl");
	}
		
	function editPpast()
	{
		$objSmarty 	= DRX::load("smarty");
		$objAlumni = new Alumni();
		$objGeneral = new AlumniGeneral();
		if ( isset($_POST["title"][0]) )
		{
			//truoc tien delete het nhung past position truoc do
			$objAlumni -> deletePastPosition($_SESSION["MEMBER_LOGIN"]["id"]);
			
			$numberJobAdd = count($_POST["title"]);
			$flgAction = false;
			for( $i=0; $i<$numberJobAdd; $i++ )
			{
				$arrInfoJob = array();
				$arrInfoJob["mb_id"] = $_SESSION["MEMBER_LOGIN"]["id"];
				$arrInfoJob["title"] = $_POST["title"][$i];
				$arrInfoJob["company"] = $_POST["company"][$i];
				$arrInfoJob["website"] = $_POST["website"][$i];
				$arrInfoJob["industry_id"] = $_POST["industry_id"][$i];
				$arrInfoJob["date_from"] = $_POST["date_from_year"][$i] . "-" . $_POST["date_from_month"][$i] . "-00 00:00:00";
				$arrInfoJob["date_to"] = $_POST["date_to_year"][$i] . "-" . $_POST["date_to_month"][$i] . "-00 00:00:00";
				if ( trim($arrInfoJob["title"]) != '' )
				{
					$flgAction = true;
					$objAlumni -> addPastPosition($arrInfoJob);
				}
			}
			if ( $flgAction )
				$objAlumni -> completionProfile('complete_past_position', 10, $_SESSION["MEMBER_LOGIN"]["id"]);
			$objSmarty->assign("STR_MSG","Action successfully!");	
		}
		
		$arrListPastPosition = $objAlumni -> getListPastPosition($_SESSION["MEMBER_LOGIN"]["id"]);
		if ( count($arrListPastPosition) )
			$objSmarty->assign("arrListPastPosition",$arrListPastPosition);
		
		$objSmarty->assign("array_industry",$objGeneral -> getListIndustry());
		$content = $objSmarty->fetch("alumni/profile/edit_position_past.tpl");
		$objSmarty->assign("content",$content);
		$objSmarty->display("solvay.tpl");
	}
	
	function changecontact($p_arrArgs)
	{
		$objAlumni = new Alumni();
		if ( $objAlumni -> changeProfileContact($p_arrArgs["form"], $_SESSION["MEMBER_LOGIN"]["id"]) )
		{
			$objAlumni -> completionProfile('complete_contact', 20, $_SESSION["MEMBER_LOGIN"]["id"]);
			return "SUCCESS";
		}
		else
			return "FAIL";
	}
	
	function editContact()
	{
		$objSmarty 	= DRX::load("smarty");
		$objAlumniCareer = new AlumniCareer();
		$objGeneral = new AlumniGeneral();
		$objAlumni = new Alumni();
		// get info contact
		$arrInfoMemberContact = $objAlumni -> getInfoMemberContact($_SESSION["MEMBER_LOGIN"]["id"]);
		if ( $arrInfoMemberContact )
			$objSmarty->assign("action","UPDATE");
		else
			$objSmarty->assign("action","ADD");
		$objSmarty->assign("arrInfoMemberContact",$arrInfoMemberContact);
		///
		$objSmarty->assign("arrListLocation",$objGeneral -> getListLocation());
		$content = $objSmarty->fetch("alumni/profile/edit_contact.tpl");
		$objSmarty->assign("content",$content);
		$objSmarty->display("solvay.tpl");
	}
	
	function changepersonal($p_arrArgs)
	{
		$objAlumni = new Alumni();
		$arrForm = $p_arrArgs["form"];
		$arrForm["mb_id"] = $_SESSION["MEMBER_LOGIN"]["id"];
		if ( $objAlumni -> updateProfilePersonal($arrForm) )
		{
			$objAlumni -> completionProfile('complete_personal', 20, $_SESSION["MEMBER_LOGIN"]["id"]);
			$arrInfoMember = $objAlumni -> getInfoMemberId($_SESSION["MEMBER_LOGIN"]["id"]);
			$_SESSION["MEMBER_LOGIN"] = $arrInfoMember;
			return "SUCCESS";
		}
		else
		{
			return "FAIL";
		}
		
	}
	function editPersonal()
	{
		$objSmarty 	= DRX::load("smarty");
		$objAlumniCareer = new AlumniCareer();
		$objGeneral = new AlumniGeneral();
		$arrInfoMember = $_SESSION["MEMBER_LOGIN"];
		if ( $arrInfoMember["dob"] != "" )
		{
			$arrInfoMember["day_dob"] = date("d",strtotime($arrInfoMember["dob"]));
			$arrInfoMember["month_dob"] = date("m",strtotime($arrInfoMember["dob"]));
			$arrInfoMember["year_dob"] = date("Y", strtotime($arrInfoMember["dob"]));
		}
		
		$objSmarty->assign("arrIntake",$objGeneral->getListIntake());
		$objSmarty->assign("arrListLocation",$objGeneral -> getListLocation());
		$objSmarty->assign("arrInfoMember",$arrInfoMember);
		$content = $objSmarty->fetch("alumni/profile/edit_personal.tpl");
		$objSmarty->assign("content",$content);
		$objSmarty->display("solvay.tpl");
	}
	
	
	function editAvatar()
	{
		if ( isset($_POST["btnSubmit"]) )
		{
			$objAlumni = new Alumni();
			$arrForm = $_POST;
			//upload hinh
			include "IO_Upload.php";
			$strPathUpload = APPLICATION_DIR . '/images/avatar';
			//echo $strPathUpload;
			$objUpload = new IO_Upload(true, $strPathUpload, 0);
			$objUpload -> setPrefixThumb(time() . "_");
			$objUpload -> uploadImage('avatar', $strNameImage);
			//echo $objUpload -> getMsgError();
			if ( $strNameImage != '' )
			{
				$objUpload -> setPrefixThumb();
				$objUpload -> createThumbnail(180, $strPathUpload, $strNameImage);	
				$objUpload -> setPrefixThumb("small_");
				$objUpload -> createThumbnail(90, $strPathUpload, $strNameImage);	
				$arrForm['avatar'] = $strNameImage;
			}//end if
			else
				$arrForm['avatar'] = '';
			
			if ( $objAlumni -> changeAvatar($_SESSION["MEMBER_LOGIN"]["id"], $arrForm['avatar']) )
			{
				/*$arrInfoMember = $_SESSION["MEMBER_LOGIN"];
				$arrInfoMember["avatar"] = $arrForm['avatar'];
				$_SESSION["MEMBER_LOGIN"] = $arrInfoMember;*/
				$objAlumni -> completionProfile('complete_avatar', 10, $_SESSION["MEMBER_LOGIN"]["id"]);
				
				$arrInfoMember = $objAlumni -> getInfoMemberId($_SESSION["MEMBER_LOGIN"]["id"]);
				$_SESSION["MEMBER_LOGIN"] = $arrInfoMember;
				
				echo '<script type="text/javascript">parent.endChangeAvatar("SUCCESS")</script>';
			}
			else
			{
				echo '<script type="text/javascript">parent.endChangeAvatar("FAIL")</script>';
			}
		}
		$smarty 	= DRX::load("smarty");
		$content = $smarty->fetch("alumni/profile/edit_avatar.tpl");
		$smarty->assign("content",$content);
		$smarty->display("solvay.tpl");
	}
	
	function profileAction()
	{
		global $arrArgs;
		$objSmarty 	= DRX::load("smarty");
		$layout     = new Layout();	
		$layout->prepareLayout($args);
		$this -> prepareLeft();
		
		$flgAction = isset($arrArgs[0]) ? $arrArgs[0] : 'edit';
		
		if ( !isset($_SESSION["MEMBER_LOGIN"]) )
		{
			redirect(APPLICATION_URL . "alumni");	
		}
		
		if ( $flgAction == "edit" )
		{
			$objSmarty -> assign("ALUMNI_ACTION", "EDIT_PROFILE");
			$actionFunction = isset($arrArgs[1]) ? $arrArgs[1] : 'avatar';
			$actionFunction = 'edit' . ucfirst($actionFunction);
			$this -> $actionFunction();
		}
		else if ( $flgAction == "completion" )
		{
			$objSmarty -> assign("ALUMNI_ACTION", "EDIT_PROFILE");
			$this -> viewProfileCompletion();
		}
		else if ( $flgAction == "changepass" )
		{
			$objSmarty -> assign("ALUMNI_ACTION", "EDIT_PROFILE");
			$this -> changePasswordAction();
		}
	}
	
	function logoutAction()
	{
		session_unregister("MEMBER_LOGIN");
		redirect(APPLICATION_URL . "alumni");
	}
	
	function loginAction()
	{
		$arrFom  = $_POST;
		$objAlumni = new Alumni();
		
		$arrInfoMember = $objAlumni -> getInfoMemberUsername(trim($arrFom['username']));
		if ( !$arrInfoMember )
		{
			$this -> setSessionMessage("Login unsuccessfully!");
		}
		else
		{
			if ( $arrInfoMember['status'] == 0 )
			{
				$this -> setSessionMessage("Account inactive, please contact administrator");
			}
			else if ( trim($arrInfoMember['password']) != md5(trim($arrFom['password'])) )
			{
				$this -> setSessionMessage("Login unsuccessfully!");
			}
			else
			{
				session_register("MEMBER_LOGIN");
				$_SESSION["MEMBER_LOGIN"] = $arrInfoMember;
			}
						
			//$this -> setSessionMessage("Login successfully!");
		}
		redirect($arrFom["redirect"]);
	}
	
	function signupAction()
	{
		$smarty 	= DRX::load("smarty");
		$objGeneral = new AlumniGeneral();
		$layout     = new Layout();	
		$layout->prepareLayout($args);
		$this -> prepareLeft();
		
		if ( isset($_POST['username']) )
		{
			$arrForm = $_POST;
			if ( $this -> doSignUp($arrForm) )
			{
				$this -> setSessionMessage("Registration successfully! Please wait for approver!");
				redirect(APPLICATION_URL . "alumni/");
			}
			else
			{
				$smarty->assign("arrForm",$arrForm);		
			}
		}
		$smarty->assign("arrIntake",$objGeneral->getListIntake());
		$content = $smarty->fetch("alumni/profile/signup.tpl");
		$smarty->assign("content",$content);
		$smarty->display("solvay.tpl");
	}

	function doSignUp($p_arrForm)
	{
		$smarty 	= DRX::load("smarty");
		$objAlumni = new Alumni();
		//kiem tra xem da ton tai username nay hay chua
		if ( $arrInfoUser = $objAlumni -> getInfoMemberUsername($p_arrForm['username']) )
		{
			$smarty->assign("STR_MSG","Username you want to registration is exist!");
			return false;
		}
		if ( $p_arrForm['mb_id'] = $objAlumni -> addMemberPersonal($p_arrForm) )
		{
			$objAlumni -> addMemberContactSignUp($p_arrForm);
			//admin thong tin member qua forum
			$this -> addMemberForum($p_arrForm);
			
			return true;
		}
		{
			$smarty->assign("STR_MSG","Action unsuccessfully!");
			return false;
		}
		
	}
	
	function changePasswordOfForum($p_strUsername, $p_strNewPassword)
	{
		$objMySQL_Read = mysql_connect(FORUM_HOST, FORUM_USERNAME, FORUM_PASSWORD); 
		if (!$objMySQL_Read) 
		{
    	die('Could not connect: ' . mysql_error());
		}
		
		mysql_select_db (FORUM_DB_NAME, $objMySQL_Read);
		$strSalt = $this -> getRandom(3);
		$strUpdate = "UPDATE forum_user 
									SET password='" . md5(md5(trim($p_strNewPassword)) . $strSalt) . "'," . 
											" salt='" . $strSalt . "'" .
									" WHERE username='" . trim($p_strUsername). "'";
		
		mysql_query($strUpdate); 
	}

	
	function addMemberForum($p_arrInfo)
	{
		$objMySQL_Read = mysql_connect(FORUM_HOST, FORUM_USERNAME, FORUM_PASSWORD); 
		if (!$objMySQL_Read) 
		{
    	die('Could not connect: ' . mysql_error());
		}
		mysql_select_db (FORUM_DB_NAME, $objMySQL_Read);
		$strSalt = $this -> getRandom(3);
		$strSql = "INSERT INTO forum_user (username, 
																			password,
																			usergroupid,
																			passworddate,
																			email,
																			showvbcode,
																			usertitle,
																			joindate,
																			lastvisit,
																			lastactivity,
																			reputation,
																			reputationlevelid,
																			options,
																			maxposts,
																			languageid,
																			autosubscribe,
																			salt																			
																			) 
																			VALUES ('" . $p_arrInfo['username']. "',". 
																			"'" . md5(md5($p_arrInfo['password']) . $strSalt) . "'," . 
																			"2, 
																			'" . date("Y-m-d", time()) . "'," . 
																			"'" . $p_arrInfo['private_email'] . "'," . 
																			"1," . 
																			"'Junior Member'," . 
																			time() . "," . 
																			time() . "," . 
																			time() . "," . 
																			"10," . 
																			"5," . 
																			"11537495, " . 
																			"-1," . 
																			"1," . 
																			"-1," .
																			"'".$strSalt."'" . 
																			")";
		//echo $strSql; die;
		mysql_query ($strSql); 
		
	}
	
	function getRandom($p_intNumber)
	{
		$characters = array(
		"A","B","C","D","E","F","G","H","J","K","L","M",
		"N","P","Q","R","S","T","U","V","W","X","Y","Z",
		"1","2","3","4","5","6","7","8","9");
		
		//make an "empty container" or array for our keys
		$keys = array();
		
		while(count($keys) < $p_intNumber) 
		{
			$x = mt_rand(0, count($characters)-1);
			if(!in_array($x, $keys)) 
			{
				 $keys[] = $x;
			}
		}
		foreach($keys as $key)
		{
   		$random_chars .= $characters[$key];
		}	
		return $random_chars;		
	}
	
	
	function prepareLeft()
	{
		$objSmarty 	= DRX::load("smarty");
		$objAlumni = new Alumni();
		global $request_url;
		$strUrlRedirect = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
		$objSmarty -> assign("strUrlRedirect", $strUrlRedirect);
		$this -> getSessionMessage();
		
		$this -> runXoadServer();
		
		if ( isset($_SESSION["MEMBER_LOGIN"]) )
		{
			$arrInfoMember = $_SESSION["MEMBER_LOGIN"];
			$arrInfoMember["completion"] = $arrInfoMember['complete_avatar']+$arrInfoMember['complete_contact']+$arrInfoMember['complete_personal']+$arrInfoMember['complete_past_position']+$arrInfoMember['complete_current_position']+$arrInfoMember['recommended']*5;
			$_SESSION["MEMBER_LOGIN"] = $arrInfoMember;
			$objSmarty -> assign("MEMBER_LOGIN", $_SESSION["MEMBER_LOGIN"]);
		}
		else if ( isset($_SESSION['MEMBER_LOGIN_SOLVAY']) )  
		{
			$arrInfoMember = $objAlumni -> getInfoMemberUsername(trim($_SESSION['MEMBER_LOGIN_SOLVAY']['username']));
			if ( !$arrInfoMember )
			{
				$this -> setSessionMessage("Login unsuccessfully!");
			}
			else
			{
				if ( $arrInfoMember['status'] == 0 )
				{
					$this -> setSessionMessage("Account inactive, please contact administrator");
				}
				else if ( trim($arrInfoMember['password']) != trim($_SESSION['MEMBER_LOGIN_SOLVAY']['password']) )
				{
					$this -> setSessionMessage("Login unsuccessfully!");
				}
				else
				{
					//$this -> setSessionMessage("Login successfully!");
					session_register("MEMBER_LOGIN");
					$_SESSION["MEMBER_LOGIN"] = $arrInfoMember;
				}
			}
			session_unregister("MEMBER_LOGIN_SOLVAY");
			redirect($strUrlRedirect);		
			
		}
	}
	
	function getSessionMessage()
	{
		$objSmarty 	= DRX::load("smarty");
		$strMsg = isset($_SESSION["MSG_SOLVAY"]) ? $_SESSION["MSG_SOLVAY"] : "";
		
		$objSmarty -> assign("MSG_SOLVAY", $_SESSION["MSG_SOLVAY"]);
		session_unregister("MSG_SOLVAY");
	}
	
	function setSessionMessage($p_strMsg)
	{
		if ( isset($_SESSION["MSG_SOLVAY"]) )
		{
			$_SESSION["MSG_SOLVAY"] = $p_strMsg;
		}
		else
		{
			session_register("MSG_SOLVAY");
			$_SESSION["MSG_SOLVAY"] = $p_strMsg;
		}
	}
	
	function indexAction()
	{
		$smarty 	= DRX::load("smarty");
		$layout     = new Layout();	
		$layout->prepareLayout($args);
		$this -> prepareLeft();
	
		$content = $smarty->fetch("alumni/home.tpl");
		$smarty->assign("content",$content);
		$smarty->display("solvay.tpl");
	}
	
	function runXoadServer()
	{
		$objSmarty = DRX::load('smarty');
		
		if ( XOAD_Server::runServer() )
		{
			exit();
		}
			
		$objSmarty -> assign("xoad_header", XOAD_Utilities::header(APPLICATION_URL . "library/external/XOAD"));	
		$objSmarty -> assign("xoad_handler", XOAD_Client::register($this));
	}	
}
?>
